package com.evl.api.ticket.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.evl.api.ticket.service.ITicketService;
import com.evl.api.ticket.vo.SearchTrainOneRQ;
import com.evl.api.ticket.vo.SearchTrainRQ;
import com.evl.api.ticket.vo.SearchTrainRS;
import com.evl.common.constant.Const;
import com.evl.common.controller.BaseController;
import com.evl.common.persistence.model.ScheduleStation;
import com.evl.common.persistence.model.TicketRemainder;
import com.evl.core.cache.CacheKit;
import com.evl.core.vo.BaseResultVo;
import com.evl.core.vo.JsonReturnHelper;
import com.evl.ticketRemainder.service.ITicketRemainderService;
import com.evl.utils.DateUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 车票管理控制器
 *
 * @author 
 * @Date 2017-06-21 15:31:32
 */
@Controller
@RequestMapping("/api/ticket")
@Api(value = "车票、线路、选座模块")
public class ApiTicketController extends BaseController {
	
	@Resource
	ITicketService ticketService;
	
    @Resource
    ITicketRemainderService ticketRemainderService;
    
    
    /**
     * 余票信息查询服务接口 （Ehcache）
     * @param departure 出发站
     * @param arrive 到达站
     * @param date 日期
     * @return 班次余票集合
     */
    @RequestMapping("/query/ticketRemainder")
    @ApiOperation(value="查询余票信息",httpMethod = "POST", response = BaseResultVo.class, notes = "查询余票信息")
    @ResponseBody
    public BaseResultVo queryTickets(@ApiParam(required = true, name = "queryTickets", value = "查询余票信息") @RequestBody SearchTrainRQ searchTrainRQ){
		
    	//查询缓存中的余票信息
    	List<SearchTrainRS> result = CacheKit.get(Const.TICKET_REMAINDER_LIST, searchTrainRQ.getDepartureCode()+"_"+searchTrainRQ.getArriveCode()+"_"+searchTrainRQ.getTravelDate());
    	
    	if(result == null || result.size() ==0){
    		
    		result = new ArrayList<SearchTrainRS>();
    		
    		List<TicketRemainder> list =  ticketRemainderService.selectTicketRemainders(searchTrainRQ.getDepartureCode(),searchTrainRQ.getArriveCode(),searchTrainRQ.getTravelDate());
    		
    		//封装数据给前端使用
    		for (TicketRemainder tr :list){
    			
    			SearchTrainRS srs = ticketService.transformSearchTrainRS(tr, false);
    			
    			result.add(srs);
    		}
    		
    		//将查询的列车余票信息存入缓存
    		CacheKit.put(Const.TICKET_REMAINDER_LIST, searchTrainRQ.getDepartureCode()+"_"+searchTrainRQ.getArriveCode()+"_"+searchTrainRQ.getTravelDate(), result);
    		
    	}
    	
    	return JsonReturnHelper.getSuccessWithData(result);
    }
    
    /**
     * 查询单条余票详情服务接口 （Ehcache）
     * @param staId 车次Id
     * @return 单条余票信息
     */
    @RequestMapping("/query/ticketRemainderOne")
    @ApiOperation(value="查询单条余票信息",httpMethod = "POST", response = BaseResultVo.class, notes = "查询单条余票信息")
    @ResponseBody
    public BaseResultVo queryTicketOne(@ApiParam(required = true, name = "queryTicketOne", value = "查询单条余票信息") @RequestBody SearchTrainOneRQ searchTrainOneRQ){
    	
    	//从缓存取
    	SearchTrainRS ticketOne = CacheKit.get(Const.TICKET_REMAINDER_DETAIL,searchTrainOneRQ.getStaId()+"_"+searchTrainOneRQ.getDepartureCode()+"_"+searchTrainOneRQ.getDepartureCode());
    	
    	if(ticketOne == null){
    		TicketRemainder tr = ticketRemainderService.selectTicketOne(Integer.parseInt(searchTrainOneRQ.getStaId()),searchTrainOneRQ.getDepartureCode(),searchTrainOneRQ.getArriveCode());
    		if(tr != null){
    			ticketOne = ticketService.transformSearchTrainRS(tr,true);
    			
    			//将单条余票详情存入缓存
    			CacheKit.put(Const.TICKET_REMAINDER_DETAIL,searchTrainOneRQ.getStaId()+"_"+searchTrainOneRQ.getDepartureCode()+"_"+searchTrainOneRQ.getArriveCode(),ticketOne);
    		}
    	}
    	
    	return JsonReturnHelper.getSuccessWithData(ticketOne);
    }
    
    /**
     * 列车站点时刻服务接口
     * @param staId 车次Id
     * @return
     */
    @RequestMapping(value ="/query/stationTime",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    @ApiOperation(value="列车站点时刻服务接口",httpMethod = "GET", response = BaseResultVo.class, notes = "查询列车站点时刻信息")
    @ResponseBody
    public BaseResultVo queryStationTime(@ApiParam(required = true, name = "queryStationTime", value = "查询列车站点时刻信息") @RequestParam("staId") String staId){
    	
    	List<ScheduleStation> list = ticketService.selectScheduleStations(Integer.parseInt(staId));
    	if(list !=null && list.size()>0){
    		for (ScheduleStation ss : list){
    			if(ss.getArrTime() !=null){
    				ss.setArrTimeStr(DateUtils.getSpecifyDate(ss.getArrTime(), DateUtils.FORMAT_HH_MM));
    			}
    			if(ss.getStartTime() !=null){
    				ss.setStartTimeStr(DateUtils.getSpecifyDate(ss.getStartTime(), DateUtils.FORMAT_HH_MM));
    			}
    		}
    	}
    	
    	return JsonReturnHelper.getSuccessWithData(list);
    }
    
}
